//
//  NDHomeTopView.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/8.
//

import SwiftUI
import Kingfisher

struct NDHomeTopView: View {
    let viewModel: NDHomeViewModel
    let isLoading: Bool
    
    var onTapClosure: ((ProductCard) -> Void)?
    
    var body: some View {
        ZStack {
            // 背景图片
            Image("top_info_bg") // 替换为你的图片名称
                .resizable()
                .scaledToFit()
            
            // 内容
            Group {
                if let productCard = viewModel.productCard {
                    let loanInterest = (productCard.focusel ?? "") + ": " + (productCard.thisfication ?? "")
                    let loanTerm = (productCard.pteraid ?? "") + ": " + (productCard.glass ?? "")
                    let buttonTitle = productCard.politward ?? ""
                    let logoUrl = URL.buildURL(with: productCard.cutad ?? "")
                    let appName = productCard.fascletterism ?? ""
                    VStack(alignment: .leading,) {
                        
                        HStack(spacing: 5) {
                            KFImage(logoUrl)
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: 20, height: 20)
                                .cornerRadius(3)
                            Text(appName)
                                .font(.system(size: 16, weight: .bold))
                                .foregroundColor(Color(hex: "#271F24"))
                        }

                        Spacer()
                        Text(productCard.majorization ?? "")
                            .font(.system(size: 58, weight: .bold))
                            .foregroundColor(Color(hex: "#FFFFFF"))
                            .minimumScaleFactor(0.3)
                            .lineLimit(1)
                        Spacer()
                        Text(loanInterest)
                            .font(.system(size: 14, weight: .medium))
                            .foregroundColor(Color(hex: "#FFFFFF"))
                        Spacer()
                        Text(loanTerm)
                            .font(.system(size: 14, weight: .medium))
                            .foregroundColor(Color(hex: "#FFFFFF"))
                        Spacer()
                        Text(buttonTitle)
                            .padding(.horizontal, 15)
                            .padding(.vertical, 10)
                            .font(.system(size: 18, weight: .bold))
                            .foregroundColor(Color(hex: "#FFFFFF"))
                            .background(Color(hex: "#271F24"))
                            .clipShape(Capsule())
                            .contentShape(Rectangle())
                            .padding(.bottom, 10)
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .background(Color.clear)
                    .shadow(radius: 2)
                }
            }
            .padding(.init(top: 35, leading: 35, bottom: 30, trailing: 20))
        }
        .onTapGesture {
            if let productCard = viewModel.productCard {
                onTapClosure?(productCard)
            }
        }
    }
}

